package com.zx.notification.admin.controller;

import com.zx.core.wrapper.ObjectWrapper;
import com.zx.core.wrapper.PageWrapper;
import com.zx.core.wrapper.ResponseWrapper;
import com.zx.log.annotation.OperateLog;
import com.zx.notification.admin.service.ISystemNotificationsService;
import com.zx.notification.common.param.SystemNotificationsParam;
import com.zx.notification.common.vo.SystemNotificationsVO;
import com.zx.validation.annotation.ParameterValidation;
import com.zx.validation.annotation.ParameterValidations;
import com.zx.validation.enums.ValidateRulesEnum;
import com.zx.validation.groups.Create;
import com.zx.validation.groups.Query;
import com.zx.validation.groups.Update;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * description: 系统通知管理控制器 <br>
 * create: 2025-10-17
 * </p>
 *
 * @author zhou  xun
 */
@RestController
@RequestMapping("/system-notifications")
@RequiredArgsConstructor
public class SystemNotificationsController {

    private final ISystemNotificationsService iSystemNotificationsService;

    /**
     * 分页查询系统通知列表
     *
     * @param systemNotificationsParam 查询参数
     * @return 系统通知列表
     */
    @PostMapping("/list")
    @PreAuthorize("hasAuthority('notify:notification:list')")
    @ParameterValidations({
            @ParameterValidation(index = 0, desc = "系统通知参数", rules = {ValidateRulesEnum.OBJECT}, groups = {Query.class})
    })
    public PageWrapper<SystemNotificationsVO> list(@RequestBody SystemNotificationsParam systemNotificationsParam) {
        return iSystemNotificationsService.getSystemNotificationsVOListByPage(systemNotificationsParam);
    }

    /**
     * 新增系统通知
     *
     * @param paramJsonStr 系统通知信息JSON字符串
     * @return {@link ResponseWrapper}
     */
    @OperateLog(value = "新增系统通知")
    @PreAuthorize("hasAuthority('notify:notification:add')")
    @PostMapping("/addSystemNotification")
    @ParameterValidations(
            @ParameterValidation(index = 0, desc = "系统通知参数", rules = {ValidateRulesEnum.OBJECT}, groups = {Create.class})
    )
    public ResponseWrapper addSystemNotification(@RequestBody String paramJsonStr) {
        // 将JSON字符串转换为参数对象
        ObjectWrapper<SystemNotificationsParam> objectWrapper = com.zx.core.utils.JsonStrUtils.checkParseObject(paramJsonStr, SystemNotificationsParam.class);
        if (objectWrapper.verifyFail()) {
            ResponseWrapper result = new ResponseWrapper();
            result.fail("参数解析失败: " + objectWrapper.getMsg());
            return result;
        }
        SystemNotificationsParam systemNotificationsParam = objectWrapper.getData();
        return iSystemNotificationsService.addSystemNotification(systemNotificationsParam);
    }

    /**
     * 编辑系统通知
     *
     * @param systemNotificationsParam 系统通知信息
     * @return {@link ResponseWrapper}
     */
    @OperateLog(value = "编辑系统通知")
    @PreAuthorize("hasAuthority('notify:notification:edit')")
    @PutMapping("/editSystemNotification")
    @ParameterValidations(
            @ParameterValidation(index = 0, desc = "系统通知参数", rules = {ValidateRulesEnum.OBJECT}, groups = {Update.class})
    )
    public ResponseWrapper editSystemNotification(@RequestBody SystemNotificationsParam systemNotificationsParam) {
        return iSystemNotificationsService.editSystemNotification(systemNotificationsParam);
    }

    /**
     * 删除系统通知
     *
     * @param systemNotificationId 系统通知Id
     * @return {@link ResponseWrapper}
     */
    @OperateLog(value = "删除系统通知")
    @PreAuthorize("hasAuthority('notify:notification:remove')")
    @DeleteMapping("/deleteSystemNotification/{systemNotificationId}")
    @ParameterValidations({
            @ParameterValidation(index = 0, desc = "系统通知Id", rules = {ValidateRulesEnum.NOT_NULL})
    })
    public ResponseWrapper deleteSystemNotification(@PathVariable("systemNotificationId") String systemNotificationId) {
        return iSystemNotificationsService.deleteSystemNotification(systemNotificationId);
    }

    /**
     * 获取指定系统通知信息
     *
     * @param systemNotificationId 系统通知Id
     * @return 系统通知信息
     */
    @OperateLog(value = "获取指定系统通知信息")
    @PreAuthorize("hasAuthority('notify:notification:detail')")
    @GetMapping("/info/{systemNotificationId}")
    public ObjectWrapper<SystemNotificationsVO> getSystemNotificationInfo(@PathVariable("systemNotificationId") String systemNotificationId) {
        return iSystemNotificationsService.selectSystemNotificationById(systemNotificationId);
    }

    /**
     * 发送系统通知
     *
     * @param systemNotificationId 系统通知Id
     * @return {@link ResponseWrapper}
     */
    @OperateLog(value = "发送系统通知")
    @PreAuthorize("hasAuthority('notify:notification:send')")
    @PostMapping("/sendSystemNotification/{systemNotificationId}")
    @ParameterValidations({
            @ParameterValidation(index = 0, desc = "系统通知Id", rules = {ValidateRulesEnum.NOT_NULL})
    })
    public ResponseWrapper sendSystemNotification(@PathVariable("systemNotificationId") String systemNotificationId) {
        return iSystemNotificationsService.sendSystemNotification(systemNotificationId);
    }
}